<?PHP use_javascript("/js/i18n/grid.locale-cn.js") ?>
<?PHP use_javascript("/js/jquery.jqGrid.min.js") ?>
<?PHP use_stylesheet("/css/ui.jqgrid.css") ?>
<?PHP include_partial("global/rightnow") ?>

<script type="text/javascript">
    $(document).ready(function(){

        $("#table_voucher_list").jqGrid({
            url: "<?PHP echo url_for("voucher/getNeedApproveVoucherList.json") ?>",
            datatype: "json",
            width: 700,
            colNames:["Id","年度","月份","编号","借方金额合计", "贷方金额合计","凭证日期","录入人员","状态","操作"],
            colModel:[
                {name: "id", hidden: true},
                {name: "acct_year"},
                {name: "mon"},
                {name: "voucher_sn"},
                {name: "debit_amount", align:"center"},
                {name: "credit_amount", align:"center"},
                {name: "date"},
                {name: "opr_name"},
                {name: "status"},
                {name: "act"}

            ],
            rowNum: 10,
            rowList: [10,20,30],
            pager: "#pager_voucher_list",
            sortname: 'id',
            viewrecords: true,
            sortorder: "desc",
            caption: "&nbsp;",
            subGrid: true,
            subGridUrl: "<?PHP echo url_for("voucher/getVoucherD.json") ?>",
            subGridModel: [{
                    name: ['摘要', '科目', '借方金额', '贷方金额'],
                    width: [80,150,60,60],
                    params: ['id']
                }],
            gridComplete: function() {
                var ids = jQuery("#table_voucher_list").jqGrid('getDataIDs');
                for(var i=0; i < ids.length; i++) {
                    var id = ids[i];
                    var btn = "<a href='#' onClick='showApproveDialog(" + id + ")'><img id='approve_voucher_" + id + "' title='审核通过该笔凭证' src='/images/icons/page_key.png' alt='审核' /></a>&nbsp;"
                    btn += "<a href='#' onClick='showCancelDialog(" + id + ")' ><img id='cancel_voucher_" + id + "' title='撤消消该笔凭证' src='/images/icons/page_delete.png' alt='撤消' /></a>&nbsp;"
                    $("#table_voucher_list").jqGrid('setRowData', ids[i], {act: btn})
                    //$("#approve_voucher_" + id).tinyTips('light', 'title')
                    //$("#cancel_voucher_" + id).tinyTips('light', 'title')
                }
            },
            loadError: function(xhr, status){
                alert( "status: " + status + " \r\n载入错误，请重登录后重试.");
                alert(xhr)
            }

        });
        $("#table_voucher_list").jqGrid('navGrid', '#pager_voucher_list', {edit:false, add:false, del:false });


    });


    function showApproveDialog(voucher_id) {
        var dialog = generateConfirmDialog("您确定要审核通过该笔会计凭证吗？", "会计凭证审核")
        $("body").append(dialog[1])

        $("#" + dialog[0]).dialog({
            resizable: false,
            height:140,
            modal: true,
            closeOnEscape: false,
            buttons: {
                '确定': function() {
                    $(this).dialog('destroy')
                    approveVoucher(voucher_id)
                },
                '取消': function() {
                    $(this).dialog('destroy')
                }
            }
        });

    }

    function showCancelDialog(voucher_id) {
        var dialog = generateConfirmDialog("您确定要撤消该笔会计凭证吗？", "会计凭证撤消")
        $("body").append(dialog[1])

        $("#" + dialog[0]).dialog({
            resizable: false,
            height:140,
            modal: true,
            closeOnEscape: false,
            buttons: {
                '确定': function() {
                    $(this).dialog('destroy')
                    cancelVoucher(voucher_id)
                },
                '取消': function() {
                    $(this).dialog('destroy')
                }
            }
        });
    }

    //审核
    function approveVoucher(voucher_id) {

        $.ajax({
            url: "<?PHP echo url_for("voucher/voucherApprove"); ?>",
            data: {"voucher_id":voucher_id,"action":"approve"},
            type: "post",
            dataType: "json",
            beforeSend: function() {
                closeAllErrorBar()
                //显示loading bar modal
                $('body').block({
                    theme: true,
                    message:'<br /><img src="/images/loadingAnimation.gif"><br />\
                      程序正在执行您请求的操作<br />请稍候片刻...<br />'
                })
            },
            success: function(data){
                //display next to do dialog
                if(data.result == 'success') {
                    var dialog = generateConfirmDialog('已成功审核');
                    $("#table_voucher_list").jqGrid('setRowData', voucher_id, {status:"已审核"})
                    $("#table_voucher_list").jqGrid('setRowData', voucher_id, {act:""})
                    
                } else {
                    var dialog = generateConfirmDialog('审核会计凭证时发生错误');
                }
                
                $("body").append(dialog[1])

                $("#" + dialog[0]).dialog({
                    resizable: false,
                    height:140,
                    modal: true,
                    closeOnEscape: false,
                    buttons: {
                        '确定': function() {
                            $(this).dialog('destroy')
                        }
                    }
                });
            },
            complete: function() {
                $('body').unblock()
            },
            error: function() {
                showErrorDialog('会计凭证审核时发生错误，可能是网络不稳定、服务器连接错误或会话超时，请重试．．．')
                showErrorBar('会计凭证审核时发生错误，可能是网络不稳定、服务器连接错误或会话超时，请重试．．．')
            }
        })
        
    }
    //撤消
    function cancelVoucher(voucher_id) {
        $.ajax({
            url: "<?PHP echo url_for("voucher/voucherApprove"); ?>",
            data: {"voucher_id":voucher_id,"action":"cancel"},
            type: "post",
            dataType: "json",
            beforeSend: function() {
                closeAllErrorBar()
                //显示loading bar modal
                $('body').block({
                    theme: true,
                    message:'<br /><img src="/images/loadingAnimation.gif"><br />\
                      程序正在执行您请求的操作<br />请稍候片刻...<br />'
                })
            },
            success: function(data){
                //display next to do dialog
                if(data.result == 'success') {
                    var dialog = generateConfirmDialog('会计凭证已成功撤消');
                    $("#table_voucher_list").jqGrid('setRowData', voucher_id, {status:"已撤消"})
                    $("#table_voucher_list").jqGrid('setRowData', voucher_id, {act:""})
                    
                } else {
                    var dialog = generateConfirmDialog('撤消会计凭证时发生错误');
                }
                
                $("body").append(dialog[1])

                $("#" + dialog[0]).dialog({
                    resizable: false,
                    height:140,
                    modal: true,
                    closeOnEscape: false,
                    buttons: {
                        '确定': function() {
                            $(this).dialog('destroy')
                        }
                    }
                });
            },
            complete: function() {
                $('body').unblock()
            },
            error: function() {
                showErrorDialog('会计凭证撤消时发生错误，可能是网络不稳定、服务器连接错误或会话超时，请重试．．．')
                showErrorBar('会计凭证撤消时发生错误，可能是网络不稳定、服务器连接错误或会话超时，请重试．．．')
            }
        })
    }

</script>

<br />
<div id="box">
    <h3>待审核会计凭证列表</h3>
    <table id="table_voucher_list" width="100%" align="center">
    </table>
    <div id="pager_voucher_list"></div>


</div>
<br />





